// Package api internal/api/router.go
package api

import (
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"gorm.io/gorm"
	_ "gozrd-admin-api/docs" // 导入生成的docs
	"time"
)

// RegisterRoutes 注册所有路由
func RegisterRoutes(r *gin.Engine, db *gorm.DB, redisClient *redis.Client) {
	// API版本
	v1 := r.Group("/api/v1")

	// Swagger文档
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 创建各模块控制器
	adminApi := NewAdminAPI(db)                                           // 新增 管理员 控制器
	departmentApi := NewDepartmentAPI(db)                                 // 新增 部门 控制器
	menuApi := NewMenuAPI(db)                                             // 新增 菜单 控制器
	postApi := NewPostAPI(db)                                             // 新增 岗位 控制器
	roleApi := NewRoleAPI(db)                                             // 新增 角色 控制器
	loginInfoApi := NewLoginInfoAPI(db)                                   // 新增 登录日志 控制器
	operationLogApi := NewOperationLogAPI(db)                             // 新增 操作日志 控制器
	authApi := NewAuthAPI(redisClient, db, time.Duration(30)*time.Minute) // 新增 认证登录 控制器

	// 注册各模块路由
	RegisterAdminRoutes(v1, adminApi)
	RegisterDepartmentRoutes(v1, departmentApi)
	RegisterMenuRoutes(v1, menuApi)
	RegisterPostRoutes(v1, postApi)                 // 注册 岗位 路由
	RegisterRoleRoutes(v1, roleApi)                 // 注册 角色 路由
	RegisterLoginInfoRoutes(v1, loginInfoApi)       // 注册 登录日志 路由
	RegisterOperationLogRoutes(v1, operationLogApi) // 注册 操作日志 路由

	RegisterAuthRoutes(v1, authApi) // 注册 认证登录 路由
	r.GET("/api/v1/auth/captcha", authApi.GetCaptcha)
	r.POST("/api/v1/auth/login", authApi.Login)

	// TODO: 注册更多模块路由
}

func RegisterAuthRoutes(r *gin.RouterGroup, api AuthAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	//authGroup := r.Group("/auth") //.Use()
	//{
	//	authGroup.GET("/captcha", api.GetCaptcha)
	//	authGroup.POST("/login", api.Login)
	//}
}

// RegisterAdminRoutes 注册 管理员 相关路由
func RegisterAdminRoutes(r *gin.RouterGroup, adminAPI *AdminAPI) {
	// 登录接口不需要认证
	// r.POST("/login", adminAPI.Login)

	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	adminGroup := r.Group("/admin").Use()
	{
		// TODO: 添加需要认证的接口
		adminGroup.POST("", adminAPI.CreateSysAdmin)
		adminGroup.GET("/info", adminAPI.GetSysAdmin)
		adminGroup.PUT("", adminAPI.UpdateSysAdmin)
		adminGroup.PUT("/updateStatus", adminAPI.UpdateSysAdminStatus)
		adminGroup.DELETE("", adminAPI.DeleteSysAdmin)
		adminGroup.PUT("/updatePassword", adminAPI.ResetSysAdminPassword)          // 管理员权限 -> 重置他人密码
		adminGroup.PUT("/updatePersonalPassword", adminAPI.UpdatePersonalPassword) // 更改个人密码
		adminGroup.PUT("/updatePersonal", adminAPI.UpdatePersonal)
		adminGroup.GET("/list", adminAPI.GetSysAdminList) //
	}
}

// RegisterMenuRoutes 注册 菜单 相关路由
func RegisterMenuRoutes(r *gin.RouterGroup, menuApi *MenuAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	menuGroup := r.Group("/menu").Use()
	{
		menuGroup.GET("/list", menuApi.GetSysMenuList)
		menuGroup.POST("", menuApi.CreateSysMenu)
		menuGroup.GET("/:id", menuApi.GetSysMenu)
		menuGroup.PUT("", menuApi.UpdateSysMenu)
		menuGroup.DELETE("", menuApi.DeleteSysMenu)
		menuGroup.GET("/list/options", menuApi.QuerySysMenuVoList)
	}
}

// RegisterDepartmentRoutes 注册 部门 相关路由
func RegisterDepartmentRoutes(r *gin.RouterGroup, deptApi *DepartmentAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	deptGroup := r.Group("/dept").Use()
	{
		deptGroup.GET("/list", deptApi.GetSysDeptList)
		deptGroup.POST("", deptApi.CreateSysDept)
		deptGroup.GET("/:id", deptApi.GetSysDeptById)
		deptGroup.PUT("", deptApi.UpdateSysDept)
		deptGroup.DELETE("", deptApi.DeleteSysDeptById)
		deptGroup.GET("/list/options", deptApi.QuerySysDeptVoList)
	}
}

// RegisterPostRoutes 注册 岗位 相关路由
func RegisterPostRoutes(r *gin.RouterGroup, postApi *PostAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	postGroup := r.Group("/post").Use()
	{
		postGroup.GET("/list", postApi.GetSysPostList)
		postGroup.POST("", postApi.CreateSysPost)
		postGroup.GET("/:id", postApi.GetSysPostById)
		postGroup.PUT("", postApi.UpdateSysPost)
		postGroup.DELETE("", postApi.DeleteSysPostById)
		postGroup.DELETE("/batch", postApi.BatchDeleteSysPost)
		postGroup.PUT("/status", postApi.UpdateSysPostStatus)
		postGroup.GET("/list/options", postApi.QuerySysPostVoList)
	}
}

// RegisterRoleRoutes 注册 角色 相关路由
func RegisterRoleRoutes(r *gin.RouterGroup, roleApi *RoleAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	roleGroup := r.Group("/role").Use()
	{
		roleGroup.GET("/list", roleApi.GetSysRoleList)
		roleGroup.POST("", roleApi.CreateSysRole)
		roleGroup.GET("/:id", roleApi.GetSysRoleById)
		roleGroup.PUT("", roleApi.UpdateSysRole)
		roleGroup.DELETE("", roleApi.DeleteSysRoleById)
		roleGroup.PUT("/status", roleApi.UpdateSysRoleStatus)
		roleGroup.GET("/list/options", roleApi.QuerySysRoleVoList)
		roleGroup.GET("/menu", roleApi.QueryRoleMenuIdList)
		roleGroup.PUT("/permission", roleApi.AssignPermissions)
	}
}

// RegisterLoginInfoRoutes 注册 登录日志 相关路由
func RegisterLoginInfoRoutes(r *gin.RouterGroup, loginInfoApi *LoginInfoAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	loginInfoGroup := r.Group("/loginInfo").Use()
	{
		loginInfoGroup.GET("/list", loginInfoApi.GetSysLoginInfoList)         // 获取登录日志列表
		loginInfoGroup.DELETE("/batch", loginInfoApi.BatchDeleteSysLoginInfo) // 批量删除登录日志
		loginInfoGroup.DELETE("", loginInfoApi.DeleteSysLoginInfoById)        // 删除单条登录日志
		loginInfoGroup.DELETE("/clean", loginInfoApi.CleanSysLoginInfo)       // 清空登录日志
	}
}

// RegisterOperationLogRoutes 注册 操作日志 相关路由
func RegisterOperationLogRoutes(r *gin.RouterGroup, operationLogApi *OperationLogAPI) {
	// 需要认证的路由组
	// authGroup := r.Group("/").Use(AuthMiddleware())
	operationLogGroup := r.Group("/operationLog").Use()
	{
		operationLogGroup.GET("/list", operationLogApi.GetSysOperationLogList)         // 获取操作日志列表
		operationLogGroup.DELETE("/batch", operationLogApi.BatchDeleteSysOperationLog) // 批量删除操作日志
		operationLogGroup.DELETE("", operationLogApi.DeleteSysOperationLogById)        // 删除单条操作日志
		operationLogGroup.DELETE("/clean", operationLogApi.CleanSysOperationLog)       // 清空操作日志
	}
}
